package com.vnp.cooking.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vnp.cooking.ChitietCongthucActivity;
import com.vnp.cooking.R;
import com.vnp.cooking.UIcontrols.MyCircleImageView;
import com.vnp.cooking.UIcontrols.MyClickAbleImageView;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.utils.PreferenceHelper;
import com.vnp.cooking.utils.Utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FriendListAdapter extends ArrayAdapter<ObjCookpad>{

	public ArrayList<ObjCookpad> objCookpads;
	Context context;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
									.cacheInMemory(false)
									.cacheOnDisc(true)
									.bitmapConfig(Config.RGB_565)
									.build();
	
	private int lastPosition = -1;
	PreferenceHelper preferenceHelper;
	
	public FriendListAdapter(Context context, ArrayList<ObjCookpad> objCookpads) {
		super(context, R.layout.canhan_list_item, objCookpads);
		this.context = context;
		this.objCookpads = objCookpads;
		preferenceHelper = new PreferenceHelper(context);
	}

	@Override
	public int getCount() {
		return objCookpads.size();
	}
	
	


	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		ViewHolder holder = null;
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.canhan_list_item, parent, false);
			holder = new ViewHolder();
			
			holder.imgAvatarUser = (MyCircleImageView) v.findViewById(R.id.img_avatar_canhan);
			holder.txtTenUser = (TextView) v.findViewById(R.id.txt_ten_user);
			holder.txtTenAction = (TextView) v.findViewById(R.id.txt_ten_action);
			holder.txtThoigiandang = (TextView) v.findViewById(R.id.txt_dang_cach_day);
			holder.imgMonan = (ImageView) v.findViewById(R.id.img_mon_an);
			holder.txtTenMonan = (TextView) v.findViewById(R.id.txt_ten_mon_an);
			holder.txtViewer = (TextView) v.findViewById(R.id.txt_viewer);
			holder.txtLiker = (TextView) v.findViewById(R.id.txt_liked_count);
			holder.txtThoigiannau = (TextView) v.findViewById(R.id.txt_thoi_gian);
			
			holder.txtTenUser.setTypeface(Utils.getTypefaceRegular(context), Typeface.BOLD);
			holder.txtTenAction.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtThoigiandang.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtTenMonan.setTypeface(Utils.getTypefaceRegular(context), Typeface.BOLD);
			holder.txtViewer.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtLiker.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtThoigiannau.setTypeface(Utils.getTypefaceRegular(context));
			
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		ObjCookpad objCookpad = objCookpads.get(position);
		// set gia tri
		ImageLoader.getInstance().displayImage(objCookpad.getUrlAvatar(), holder.imgAvatarUser, options);
		holder.txtTenUser.setText(preferenceHelper.getFriendName());
		
		// lay thoi gian hien thi
		String timeAgo = Utils.getTimeAgoAsString(Utils.getCurrentTime() - Utils.convertStringToLong(objCookpad.getCreatedDateCookpad()));
		
		holder.txtThoigiandang.setText(timeAgo);
		
		
		ImageLoader.getInstance().displayImage(objCookpad.getUrlImageCookpad(), holder.imgMonan, options);
		if (objCookpads.get(position).getNameCookpad().length() > 20) {
			holder.txtTenMonan.setText(objCookpads.get(position).getNameCookpad().substring(0, 20)+ "...");
		} else {
			holder.txtTenMonan.setText(objCookpad.getNameCookpad());
		}
		
		holder.txtViewer.setText(""+objCookpad.getViewedCookpad());
		holder.txtLiker.setText(""+objCookpad.getLikeCookpad());
		holder.txtThoigiannau.setText(objCookpad.getThoigianNauan());
		// check action de hien thi
		String action = objCookpad.getAction();
		if (action.matches("like")) {
			holder.txtTenAction.setText("Đã thích công thức");
		} else if (action.matches("rating")) {
			holder.txtTenAction.setText("Đã đánh giá công thức");
		} else if (action.matches("comment")) {
			holder.txtTenAction.setText("Đã bình luận công thức");
		} else if (action.matches("create")) {
			holder.txtTenAction.setText("Đã tạo công thức");
		} else if (action.matches("pin")) {
			holder.txtTenAction.setText("Đã thêm bộ sưu tập");
		}
		
		
		holder.imgMonan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent c = new Intent(context, ChitietCongthucActivity.class);
				c.putExtra("objCookpad", objCookpads.get(position));
				v.getContext().startActivity(c);
			}
		});
		
		// Animation
		Animation animation = AnimationUtils.loadAnimation(getContext(), (position > lastPosition) ? R.anim.listview_up_from_bottom : R.anim.listview_down_from_bottom);
		v.startAnimation(animation);
		lastPosition = position;

		
		return v;
	}
	
	
	// optimize
	class ViewHolder{
		MyCircleImageView imgAvatarUser;
		TextView txtTenUser;
		TextView txtTenAction;
		TextView txtThoigiandang;
		ImageView imgMonan;
		TextView txtTenMonan;
		TextView txtViewer;
		TextView txtLiker;
		TextView txtThoigiannau;
		
	}

}
